﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.IO;
namespace NhanAi
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

           
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Application.Lock();     
            int count = 1;
    
                string filename=Server.MapPath("/App_data") + "\\count.txt";
                if (File.Exists(filename))
                {
                    string num = File.ReadAllText(filename);                    
                    count = Home.Utils.TypeWrapper.GetInt32(num);
                    count++;
              
                    File.WriteAllText(filename, count.ToString());
                }
                else
                {                 
                    File.WriteAllText(filename,"1");
                }
                Application["view_count"] = count;
        
            //online
            if (Application["online"] == null)
            {
                Application["online"] = 1;
            }
            else
            {
                int num =(int) Application["online"];
                num++;
                Application["online"] = num;
            }
            Application.UnLock();
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.User != null)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {

                    if (HttpContext.Current.User.Identity.AuthenticationType != "Forms")
                        throw new Exception("Only forms authentication is supported, not " +
                            HttpContext.Current.User.Identity.AuthenticationType);

                    System.Security.Principal.IIdentity userId = HttpContext.Current.User.Identity;

                    //if role info is already NOT loaded into cache, put the role info in cache
                    if (System.Web.HttpContext.Current.Cache[userId.Name] == null)
                    {
                       // userId.
                        string role = "";
                        string[] info = userId.Name.Split('_');
                        if (info.Length >= 2) role = info[info.Length - 1];
                        string[] roles = new string[] { role };

                        roles = new string[] { role };
                       // 1 hour sliding expiring time. Adding the roles in chache. This will be used in Application_AuthenticateRequest event located in Global.ascx.cs file to attach user Principal object.
                        System.Web.HttpContext.Current.Cache.Add(userId.Name, roles, null, DateTime.MaxValue, TimeSpan.FromHours(1), System.Web.Caching.CacheItemPriority.BelowNormal, null);
                    }

                    //now assign the user role in the current security context
                    HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(userId, (string[])System.Web.HttpContext.Current.Cache[userId.Name]);
                }
            }
        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            //online
            Application.Lock();
            if (Application["online"] == null)
            {               
                Application["online"] = 0;
            }
            else
            {
                int num = (int)Application["online"];
                num--;
                Application["online"] = num;
            }
            Application.UnLock();
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}